package com.ts.view

import android.bluetooth.le.ScanFilter
import android.content.Context
import android.graphics.SurfaceTexture
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import androidx.camera.core.Preview
import androidx.lifecycle.LifecycleOwner
import com.ts.opengl.ScreenFilter
import com.ts.utils.CameraXHelper
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class CameraXView:GLSurfaceView, GLSurfaceView.Renderer, Preview.OnPreviewOutputUpdateListener,
    SurfaceTexture.OnFrameAvailableListener {


        lateinit var screenFilter:ScreenFilter

    private var mCameraTexure: SurfaceTexture? = null
    constructor(context:Context):super(context){

    }

    constructor(context: Context,attrs:AttributeSet) :super(context,attrs){
        setEGLContextClientVersion(2)
        setRenderer(CameraXSurfaceView(this))
        renderMode=GLSurfaceView.RENDERMODE_WHEN_DIRTY
        initCamera()
    }

    private fun initCamera() {
        var mCameraXUtil= CameraXHelper(context as LifecycleOwner,this);

    }
    //纹理id
    private val textures = 0
    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        var version=gl?.glGetString(GL10.GL_VERSION)

        mCameraTexure?.attachToGLContext(textures)

        mCameraTexure?.setOnFrameAvailableListener(this)

        screenFilter= ScreenFilter(context);
    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {

    }

    override fun onDrawFrame(gl: GL10?) {

//        gpu       确定形状    上色
        mCameraTexure?.updateTexImage()
    }

    override fun onUpdated(output: Preview.PreviewOutput?) {
        mCameraTexure=output?.surfaceTexture;
    }

    override fun onFrameAvailable(surfaceTexture: SurfaceTexture?) {
        //手动刷新
        requestRender();
    }
}